#include "../socket/mysocket.h"
#include "../cmd/cmd.h"
#include "../cmd/cmdfactor.h"

using std::cout;using std::endl;

struct cmdfactors
{
    logincmdfactor logincmdf;
    logoutcmdfactor logoutcmdf;
    cmdfactors(){
        logincmdf=logincmdfactor();
        logoutcmdf=logoutcmdfactor();
    };

    cmd* getcmdptr(string cmdline)
    {
        if(cmdline==string("")) {
            cout<<"empty line recved\n";
            return nullptr;
        }else{
            istringstream iss(cmdline);
            string token;
            iss>>token;
            if(token==string("login"))
            {
                cmd* cmdptr = logincmdf.getcmd(cmdline);
                return cmdptr;
            }else if(token==string("logout"))
            {
                cmd* cmdptr = logoutcmdf.getcmd(cmdline);
                return cmdptr;
            }else{
                cout<<"wrong cmd recved\n";
                return nullptr;
            }

        }


    }
};

int main()
{

    cmdfactors cfs;
    cout<<"<<<<<<<<<<<<<\n";

    cSocket s("127.0.0.1",8080);
    s.init();

    string str;
    while(1)
    {
        
        getline(cin,str);
        if(cmd* cmdptr = cfs.getcmdptr(str))
        {
            cout<<"check pass\n";
            if(cmdptr->isvalid()){
                s.sendmsg(str);
            }else{
                cout<<"cmd invalid\n";
            }
        }else{
            cout<<"check failed\n";
        }



    }
    cout<<"<<<<<<<<<<<<<\n";
    return 0;
}